package com.itboye.ihomebank.activity.home;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.HouseZiDianBean;
import com.itboye.ihomebank.bean.HouseZiDianBean.housePay;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.CaculateYearAndMonth;
import com.itboye.ihomebank.util.MyGongJv;
import com.itboye.ihomebank.util.PayTypePopupWindow;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.TimesUtils;
import com.itboye.ihomebank.util.YuYueChooseTimePopwindow;
import com.itboye.ihomebank.util.YuYueChooseTimePopwindow.onFinishedLisenter;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 申请签约
 */
public class ActivityShenQingZuFang extends BaseOtherActivity implements
		OnClickListener, Observer, onFinishedLisenter {
	View v_statusbar;
	Button btn_submit;
	EditText edit_content;
	TextView add_shap_title_tv;
	ImageView close_icon;
	HousePresenter housePresenter;
	private String houseNo;
	private String uid;
	private EditText edit_zujin;
	private String money;
	private TextView timestart;
	private String timeStart;
	private TextView timeend;
	private String timeEnd;
	private String fuKuan;
	private TextView fukuan;
	RelativeLayout fukuanfangshi, startTime, stopTime;
	private PayTypePopupWindow payTypePopupWindow;
	long currentTime;
	private long startT;
	private long endT;

	private List<housePay> house_pay = new ArrayList<housePay>();// 支付方式
	protected String fangshi;
	private OptionsPickerView pvOptionsZhiFu;
	private ArrayList<String> options1ItemsZhiFu = new ArrayList<>();//支付方式
	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.btn_submit:
			money = edit_zujin.getText().toString();
			if (money.equals("")) {
				ByAlert.alert("请输入意向金额");
				return;
			}
			timeStart = timestart.getText().toString();
			if (timeStart.equals("")) {
				ByAlert.alert("请选择起租日期");
				return;
			}
			timeEnd = timeend.getText().toString();
			if (timeEnd.equals("")) {
				ByAlert.alert("请选择止租日期");
				return;
			}
//			if (currentTime > startT) {
//				ByAlert.alert("起租起始时间应该大于当前时间");
//				return;
//			}
			System.out.println(startT + ">?:" + endT);
			if (endT <= startT) {
				ByAlert.alert("止租时间应该大于起租时间");
				return;
			}
			fuKuan = fukuan.getText().toString();
			if (fuKuan.equals("")) {
				ByAlert.alert("请选择付款方式");
				return;
			}
			houseNo = getIntent().getStringExtra("house_no");
			uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";

			housePresenter.shenQingQianYue(uid, houseNo, "", "0", timeStart,
					timeEnd, money, fuKuan);
			// intent=new Intent(this,ActivityShenQingZuFang.class);
			// startActivity(intent);
			break;
		case R.id.fukuanfangshi:// 选择付款方式
			MyGongJv.yincang(v);
			pvOptionsZhiFu.show();
			break;
		case R.id.stopTime:
			MyGongJv.yincang(v);
			YuYueChooseTimePopwindow endTimePopwindow = new YuYueChooseTimePopwindow(
					this, this, false);
			endTimePopwindow.getContentView().findViewById(R.id.time).setVisibility(View.GONE);
			endTimePopwindow.getContentView().findViewById(R.id.min).setVisibility(View.GONE);
			endTimePopwindow.showAtLocation(v, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
			
			
			break;
		case R.id.startTime:
			MyGongJv.yincang(v);
			YuYueChooseTimePopwindow startTimePopwindow = new YuYueChooseTimePopwindow(
					this, this, true);
			startTimePopwindow.getContentView().findViewById(R.id.time).setVisibility(View.GONE);
			startTimePopwindow.getContentView().findViewById(R.id.min).setVisibility(View.GONE);
			startTimePopwindow.showAtLocation(v, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
			break;
		default:
			break;
		}
	}

	/**
	 * 支付方式
	 */
	private void initOptionPicker() {//条件选择器初始化

		/**
		 * 注意 ：如果是三级联动的数据(省市区等)，请参照 JsonDataActivity 类里面的写法。
		 */
		pvOptionsZhiFu = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
			@Override
			public void onOptionsSelect(int options1, int options2, int options3, View v) {
				//返回的分别是三个级别的选中位置
				String tx = options1ItemsZhiFu.get(options1);
				fukuan.setText(tx);
				fangshi = house_pay.get(options1).getId();
			}
		})
				.setTitleText("付款方式选择")
				.setContentTextSize(16)//设置滚轮文字大小
				.setTitleSize(16)//设置标题文字大小
				.setSubCalSize(16)//设置确定按钮大小
				.setSelectOptions(0, 1)//默认选中项
				.setLineSpacingMultiplier(2.0f)//设置间距倍数,但是只能在1.2-2.0f之间
				.setDividerColor(Color.LTGRAY)//设置分割线的颜色
				.setBgColor(Color.WHITE)//滚轮背景颜色
				.setTitleBgColor(Color.WHITE)//标题背景颜色
				.setTitleColor(Color.BLACK)//标题颜色
				.setCancelColor(Color.BLACK)//取消按钮颜色
				.setSubmitColor(Color.BLACK)//确定按钮颜色
				.setTextColorCenter(Color.BLACK)//分割线之间的文字颜色
				.isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
				.setLabels("", "", "")
				.setBackgroundId(0x66000000) //设置外部遮罩颜色
				.build();
		pvOptionsZhiFu.setPicker(options1ItemsZhiFu);//二级选择器
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_reson;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		setStatusBarHeight(v_statusbar);
		 currentTime=System.currentTimeMillis();
		add_shap_title_tv.setText("提交申请");
		housePresenter = new HousePresenter(this);
		housePresenter.houseZiDian("");
		initOptionPicker();
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.qianYueShenQing_success) {
				ByAlert.alert("申请成功");
				finish();
			} else if (entity.getEventType() == HousePresenter.qianYueShenQing_fail) {
				ByAlert.alert(entity.getMsg());
			}else if (entity.getEventType() == HousePresenter.houseZiDian_success) {
				HouseZiDianBean houseZiDianBean = (HouseZiDianBean) entity
						.getData();
				house_pay.addAll(houseZiDianBean.getHouse_pay());
				for (int i = 0; i < house_pay.size(); i++) {
					options1ItemsZhiFu.add(house_pay.get(i).getName());
				}
			}
		}
	}

	@Override
	public void getBeginTime(String year, String month, String day,
			String hour, String minite) {
		// TODO Auto-generated method stub
		startT = CaculateYearAndMonth.parseDate(year + "-" + month + "-" + day);
		timestart.setText(year + "年" + month + "月" + day + "日");
	}

	@Override
	public void getEndTime(String year, String month, String day, String hour,
			String minite) {
		// TODO Auto-generated method stub
		endT = CaculateYearAndMonth.parseDate(year + "-" + month + "-" + day);
		timeend.setText(year + "年" + month + "月" + day + "日" );
	}

}
